//
//  ShellProfitView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/23.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellProfitView.h"
#import "ShellProfitHeadView.h"
#import "ShellProfitCell.h"
#import "LZBaseTableView.h"
#import "ShellProfitViewModel.h"


@interface  ShellProfitView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) ShellProfitViewModel *viewModel;

@property (nonatomic,strong) LZBaseTableView *tableView;
@property (nonatomic,strong) ShellProfitHeadView *headView;


@end

@implementation ShellProfitView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellProfitViewModel *)viewModel;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    self.headView = [[ShellProfitHeadView alloc] initViewModel:self.viewModel];
    self.headView.frame = CGRectMake(0, 0, K_SCREENWIDTH, 243);
    
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.bottom.mas_equalTo(0);
    }];
    self.tableView.tableHeaderView = self.headView;
    
    @weakify(self);
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self reloadRecordData:[x integerValue]];
        
    }];
    
    [self.tableView addAutoNormalFooterWithRefreshingBlock:^{
        @strongify(self);
        [self.viewModel getHistoryProfit:NO];
    }];
    
}


//刷新数据
- (void)reloadRecordData:(NSInteger)type{
    
    if(type > 3)return;


    self.tableView.mj_header.state = MJRefreshStateIdle;
    self.tableView.mj_footer.state = MJRefreshStateIdle;
    [self.tableView reloadData];
    [self.tableView dismissEmptyView];
    
    switch (type) {
        case 0:{
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
            break;
        default:
            break;
    }
    if(self.viewModel.dataArray.count == 0){
        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
    }
}




#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShellProfitCell *profitCell = [tableView dequeueReusableCellWithIdentifier:@"ShellProfitCell"];
    if(self.viewModel.dataArray.count > indexPath.row){
        profitCell.indexPath = indexPath;
        profitCell.modelObject = self.viewModel.dataArray[indexPath.row];
    }
    return profitCell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *sectionView = [[UIView alloc] init];
    sectionView.backgroundColor = COLOR_BACKGROUND;
    sectionView.frame = CGRectMake(0, 0, K_SCREENWIDTH, 40);
    //数量
    UILabel *timeLab = [UILabel lz_labelWithText:LZLocalizedString(@"时间") fontSize:k_textFont(12) color:UIColorHex(0x678DA8)];
    timeLab.frame = CGRectMake(20, 0, 35, 40);
    [sectionView addSubview:timeLab];
    
    //数量
    UILabel *numberLab = [UILabel lz_labelWithText:LZLocalizedString(@"数量") fontSize:k_textFont(12) color:UIColorHex(0x678DA8)];
    numberLab.frame = CGRectMake(110, 0, 35, 40);
    [sectionView addSubview:numberLab];
    
    //订单金额
    UILabel *amountLab = [UILabel lz_labelWithText:LZLocalizedString(@"订单金额") fontSize:k_textFont(12) color:UIColorHex(0x678DA8)];
    amountLab.frame = CGRectMake(150, 0, 60, 40);
    [sectionView addSubview:amountLab];
    
    
    //订单收益
    UILabel *orderProfitLab = [UILabel lz_labelWithText:LZLocalizedString(@"订单收益") fontSize:k_textFont(12) color:UIColorHex(0x678DA8)];
    orderProfitLab.frame = CGRectMake(220, 0, 55, 40);
    [sectionView addSubview:orderProfitLab];
    
    //矿池奖励
    UILabel *rewardLab = [UILabel lz_labelWithText:LZLocalizedString(@"矿池奖励") fontSize:k_textFont(12) color:UIColorHex(0x678DA8)];
    rewardLab.frame = CGRectMake(285, 0, K_SCREENWIDTH - 20 - 285, 40);
    [sectionView addSubview:rewardLab];
    return sectionView;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}




#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = COLOR_BACKGROUND;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ShellProfitCell class] forCellReuseIdentifier:@"ShellProfitCell"];
        _tableView.rowHeight = 50;
    }
    return _tableView;
}


@end
